//
//  SubjectUploadPicTableViewCell.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/11/10.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "SubjectUploadPicTableViewCell.h"
#import "UILabel+NSAttributeString.h"
#import "PhotoUploadCollectionViewCell.h"

@interface SubjectUploadPicTableViewCell ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>


@property(nonatomic,strong)UICollectionView *PhotoCollection;

@end
@implementation SubjectUploadPicTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
 
        UICollectionViewFlowLayout *layOut=[[UICollectionViewFlowLayout alloc]init];
        
        layOut.scrollDirection=UICollectionViewScrollDirectionVertical;//垂直滚动
        
        layOut.sectionInset=UIEdgeInsetsMake(5, 15, 10, 15);
        
        self.PhotoCollection=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 11, SCREEN_WIDTH,100) collectionViewLayout:layOut];
        
        self.PhotoCollection.backgroundColor=[UIColor whiteColor];
        
        self.PhotoCollection.delegate=self;
        
        self.PhotoCollection.dataSource=self;
        
        self.PhotoCollection.scrollEnabled=NO;
        
        [self.PhotoCollection registerNib:[UINib nibWithNibName:@"PhotoUploadCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"Pcell"];
        
        [self.contentView addSubview:self.PhotoCollection];
        
    }
    
    return self;
}


-(void)setImageArr:(NSArray *)ImageArr
{
    
    if (self.isEdit) {
        //如果可编辑，加个上传图标
        NSMutableArray *addPhoto=[[NSMutableArray alloc]initWithArray:ImageArr];
        
        [addPhoto addObject:@"AddnewPhoto"];
        
        _ImageArr=addPhoto;
        
        NSInteger colorm=(_ImageArr.count-1)/3+1;//每行放三个，一个有多少行
        
        CGFloat cellHeight=(SCREEN_WIDTH-30-10*2)/3.0;
        
        self.PhotoCollection.frame=CGRectMake(0, 11, SCREEN_WIDTH, 15+10*(colorm-1)+colorm*cellHeight);
        
    }else{
        
        _ImageArr=ImageArr;
        
        if (ImageArr.count==0) {
            
            self.PhotoCollection.frame=CGRectMake(0, 11, SCREEN_WIDTH, 0);
            
        }else{
            
            NSInteger colorm=(_ImageArr.count-1)/3+1;//每行放三个，一个有多少行
            
            CGFloat cellHeight=(SCREEN_WIDTH-30-10*2)/3.0;
            
            self.PhotoCollection.frame=CGRectMake(0, 11, SCREEN_WIDTH, 15+10*(colorm-1)+colorm*cellHeight);
            
        }
        
    }
    
    
    
    
    [self.PhotoCollection reloadData];
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return 1;
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.ImageArr.count;
    
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoUploadCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"Pcell" forIndexPath:indexPath];
    
    if (self.isEdit) {
        
        if (indexPath.item<self.ImageArr.count-1) {
            
            [cell.PhotoImageView LoadWebImageWithName:self.ImageArr[indexPath.item] andIsOrgin:NO];
            
            cell.DelectBtn.hidden=NO;
        }else{
            
            cell.PhotoImageView.image=[UIImage imageNamed:self.ImageArr[indexPath.row]];
            
            
            cell.DelectBtn.hidden=YES;
        }
        
        
        
    }else{
        
        [cell.PhotoImageView LoadWebImageWithName:self.ImageArr[indexPath.item] andIsOrgin:NO];
        
        cell.DelectBtn.hidden=YES;
        
    }
    
    
    cell.NotiBlock=^(UICollectionViewCell *Rcell)
    {
        PhotoUploadCollectionViewCell *delecell=(PhotoUploadCollectionViewCell*)Rcell;
        
        [self DeleBlockcell:delecell];
        
    };
    
    
    return cell;
    
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    
    return 10;
    
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    
    return 10;
    
    
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGSize size=CGSizeMake((SCREEN_WIDTH-30-10*2)/3.0, (SCREEN_WIDTH-30-10*2)/3.0);
    
    return size;
    
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.isEdit) {
        
        if (indexPath.item<self.ImageArr.count-1) {
            
            _ScanBlock(indexPath.item);
            
        }else{
            
            _UploadBlock();
            
        }
        
    }else{
        
        _ScanBlock(indexPath.item);
        
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.selectionStyle=UITableViewCellSelectionStyleNone;
    // Initialization code
}

-(void)DeleBlockcell:(PhotoUploadCollectionViewCell*)cell{
    
    NSIndexPath *index=[self.PhotoCollection indexPathForCell:cell];
    
    _delePhoto(index.item);
    
}





- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
